
use "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/temp.dta", clear

local less_observable_vars  ductravg duct_type negleakavg ///
		rsavg atticrvalue eeravg notwinrec ah_type

local more_observable_vars sizeavg progtherm ///
		fftype whftype negsysageavg  ah_location  notraddr twosystems

*verify that predicted scores are supposed to have variances equal to the variances 
*presented in the table.
preserve
	tt `less_observable_vars',  cut(2) components(1)
	ttpredict Less_1
	sum Less_1
restore
	
preserve

	cap drop Less More_1 More_2
	*Cut level 2 example less
	tt `less_observable_vars',  cut(2) components(1)

	ttpredict Less_1 Less_2 Less_3 Less_4 Less_5 Less_6 Less_7 Less_8 
	return list
	ereturn list
	mat basis=e(basis)
	*e(energy gives the eigenvalues)
	*e(energyadj gives the adjusted variances)

	*e(basis) gives the component loadings. They can also be obtained by doing this:

	*for scoring, can do this:
	reg Less_1  ductravg duct_type negleakavg ///
			rsavg atticrvalue eeravg

	*so let's try to construct Less_1

	gen can_i_construct= basis[1,1]*ductravg+ basis[3,1]*negleakavg+basis[5,1]*atticrvalue
	sum can_i_construct
	sum Less_1
	
	*totally worked:
	corr can_i_construct Less_1


	pca   ductravg duct_type negleakavg ///
			rsavg atticrvalue eeravg , factors(1)
	predict pca_Less_1, rotate

	corr pca_Less_1 ductravg

	corr pca_Less_1  Less_1

	*not equal to correlations because factors are correlated.
	corr Less_1 ductravg

	tt `more_observable_vars', cut(3) components(2)

	ttpredict More_1 More_2 More_3 More_4 More_5 More_6 More_7 More_8
	return list
	ereturn list

	mat li e(basis)

	*need to save the loadings so that we can construct variables using our 
	*estimation data

	reg More_1 fftype whftype

	gen fftype_More_Fuel=_b[fftype]
	gen whftype_More_Fuel=_b[whftype]

	reg More_2 progtherm ah_location twosystems

	gen ah_location_More_Other=_b[ah_location]
	gen progtherm_More_Other=_b[progtherm]
	gen twosystems_More_Other=_b[twosystems]

	reg Less_1 ductravg negleakavg atticrvalue

	gen ductravg_Less=_b[ductravg]
	gen negleakavg_Less=_b[negleakavg]
	gen atticrvalue_Less=_b[atticrvalue]

	gen dum=1
	keep dum fftype_More_Fuel whftype_More_Fuel ah_location_More_Other progtherm_More_Other ///
		twosystems_More_Other ductravg_Less negleakavg_Less atticrvalue_Less
		
	keep if _n==1
		
	save "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/load_tt_from_test.dta", replace
restore




*so basically, the factors are constructed easily by just adding the weightings from the loadings.

*get correct matrix dimensions
gen dum=1
reg dum `less_observable_vars' `more_observable_vars' , nocons
ereturn list

mat b=e(b)'

mat basis_mat=[b, b, b]
mat li basis_mat

local components_less=1
local components_more=2
local cut_less=2
local cut_more=3
ereturn list 

mat components_variance=[1, 1, 1]

*g counts the total number of components 
local g=1
foreach descriptor in less more {
	di "descriptor=``descriptor'_observable_vars'"
	di "cut=`cut_`descriptor''"
	di "components=`components_`descriptor''"
	tt ``descriptor'_observable_vars', cut(`cut_`descriptor'') components(`components_`descriptor'')
	mat li e(basis)
	mat li e(energy)
	
	
	
	
	local cols_min=`g'
	local cols_max=`g'+`components_`descriptor''-1
	forval c=`cols_min'/`cols_max' {
		
		di "c=`c'"
		di "g=`g'"
		*local energy=e(energy)[1, `c']
		*di "energy=`energy'"
		local pos_in_component_mat=`c' - `cols_min'+1
		mat components_variance[1,`g']=e(energy)[`pos_in_component_mat',1]

		*k counts all variables
		local k=1
		*n counts variables in the component.
		local n=1
		di "good till here"
		foreach var in `less_observable_vars' `more_observable_vars' {
			if regexm("``descriptor'_observable_vars'","`var'")>0 {
				di "HEYO"

				mat basis_mat[`k',`g'] = e(basis)[`n',`pos_in_component_mat']
				local n=`n'+1
				di "n=`n'"
				}
			else {
				local pos_in_component_mat=`c' - `cols_min'+1

				mat basis_mat[`k',`g']=0
				}
			local k=`k'+1
			di "k=`k'"
			}
		local g=`g'+1

		mat li basis_mat

	}	

}


estadd matrix basis_mat

estadd matrix components_variance


la var negleakavg "- \% Duct Leakage"
*first output the factor matrix without the bottom
esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/tt_main.tex", replace fragment ///
cells("basis_mat[1](t label(\multicolumn{1}{c}{}) fmt(3)) basis_mat[2](t label(\multicolumn{1}{c}{})) basis_mat[3](t label(\multicolumn{1}{c}{}))") ///
		 label booktabs noobs nomtitles nonumbers nolines  se(4) b(4) ///
			sfmt( %9.0fc 3 3 0 0  ) star(* 0.10 ** 0.05 *** 0.01) ///
		substitute(0.000 "" )


		
*now output the bottom
esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/tt_bottom.tex", replace fragment ///
cells("basis_mat[1](t label(\multicolumn{1}{c}{}) fmt(3)) basis_mat[2](t label(\multicolumn{1}{c}{})) basis_mat[3](t label(\multicolumn{1}{c}{}))") ///
		keep( ) ///
		nolines ///
		 label booktabs noobs nomtitles nonumbers  se(4) b(4) ///
			sfmt( %9.0fc 3 3 0 0  ) star(* 0.10 ** 0.05 *** 0.01) ///
			scalars( ///
	 "N Observations" ///
			)  ///
		substitute(0.000 "" )
		
*now output the components_variance
esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/tt_components_variance.tex", replace fragment ///
cells("components_variance[1](t label(\multicolumn{1}{c}{}) fmt(3)) components_variance[2](t label(\multicolumn{1}{c}{})) components_variance[3](t label(\multicolumn{1}{c}{}))") ///
		 label booktabs noobs nomtitles nonumbers nolines  se(4) b(4) ///
			sfmt( %9.0fc 3 3 0 0  ) star(* 0.10 ** 0.05 *** 0.01) ///
		substitute(r1 "Component Variance" )
		

		